import { defineStore } from 'pinia'
import { computed, reactive, ref } from 'vue'

export const useUserStore = defineStore('user', () => {
  /** state */
  const token = ref<string>('') // token
  const permissionList = ref<string[]>([]) // 权限
  const userInfo = reactive({
    username: '',
    role: '',
  }) // 用户信息

  /** getter */
  const isLogin = computed(() => Boolean(token.value))

  /** action */
  // 设置token
  function setUserToken(value: string) {
    token.value = value
  }

  const setPermission = (list: any[]) => {
    const ids: string[] = []
    list.forEach((item) => {
      ids.push(item.menuUrl)
    })
    permissionList.value = ids
  }

  // 导出
  return { token, isLogin, userInfo, permissionList, setUserToken, setPermission }
})
